var app = getApp();
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    session_key: '',
    imgurl: app.globalData.url
  },
  authorization: function (e) {
    var th=this
    // 查看是否授权
    wx.getSetting({
      success(res) {
        // if (res.authSetting['scope.userInfo']) {
        //   // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          
        // } else {
        //   console.log("未授权，需要从新授权");
        // }
        wx.getUserInfo({
          success(res) {
            /*解密会员信息*/
            var that = this;
            wx.request({
              url: app.globalData.appletUrl + "/applet/decode",
              data: {
                sessionKey: th.data.session_key,
                encryptedData: res.encryptedData,
                ivStr: res.iv,
                shopId: app.globalData.shopId,
                parentId:wx.getStorageSync('parentId')
              },
              success: function (res) {
                console.log(res);
                wx.setStorage({
                  key: 'memberInfo',
                  data: res.data.member
                });
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }
        })





      }
    })
  },
  onLoad() {
  },
  bindGetUserInfo(e) {

    console.log();


    var that=this
    app.globalData.nickName = e.detail.userInfo.nickName
    // 登录
    wx.login({
      success: res => {

       

          // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.request({
          url: app.globalData.appletUrl + "/applet/getSessionKey",
          data: {
            code: res.code,
            shopId: app.globalData.shopId,
          },
          success: function (res) {
            // wx.setStorageSync("session_key", res.data.session_key)

            console.log("拿到的 sessionKey:"+res.data.session_key);

            

            that.setData({
              session_key: res.data.session_key
            })
            that.authorization()
          }
       })
      }
    })
  }
})